package Presum;

import org.junit.Test;

import java.util.HashMap;

/**
 * @Classname 和为K的子数组560
 * @Description TODO
 * @Date 2023/2/15 21:56
 * @Created by xjl
 */
public class 和为K的子数组560 {

    public int subarraySum(int[] nums, int k) {
        int count = 0, pre = 0;
        HashMap < Integer, Integer > mp = new HashMap < > ();
        mp.put(0, 1);
        for (int i = 0; i < nums.length; i++) {
            pre += nums[i];
            if (mp.containsKey(pre - k)) {
                count += mp.get(pre - k);
            }
            mp.put(pre, mp.getOrDefault(pre, 0) + 1);
        }
        return count;
    }

    @Test
    public void test(){
        int i = subarraySum(new int[]{1, 1,1}, 2);
        System.out.println(i);
    }
}